package com.jessechanrui.template.example;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.jessechanrui.template.FileUtil;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author chenrui
 * @date 2025/11/20
 * @description:
 */
public class ExamplePdfPTable {
    public static void main(String[] args) throws DocumentException, IOException {
        Rectangle rect = new Rectangle(PageSize.A4);//文档竖向
        Document doc = new Document(rect);
        PdfWriter writer= PdfWriter.getInstance(doc, new FileOutputStream(new File(FileUtil.getOutDir(), "ExamplePdfPTable.pdf")));

        writer.setPdfVersion(PdfWriter.VERSION_1_7);
        //页边空白
        doc.setMargins(40, 40, 40, 40);
        //打开文档
        doc.open();

        List<List<String>> items = new ArrayList<>();
        List<String> colName = new ArrayList<>();
        for(int i=0; i<5;i++){
            colName.add("列名"+i);
            ArrayList<String> item = new ArrayList<>();
            for(int j=0; j<10;j++){
                item.add("内容"+i+"-"+j);
            }
            items.add(item);
        }

        doc.add(createTable(colName, items));
        doc.close();
        writer.close();
    }

    private static PdfPTable createTable(List<String> colName, List<List<String>> items) throws DocumentException, IOException {
        PdfPTable inttable = new PdfPTable(5);
        inttable.setWidthPercentage(100);//表格100%宽度填充
        inttable.setLockedWidth(false);//不固定宽度

        inttable.getDefaultCell().setLeading(1f, 1f);//单元格内文字行间距
        inttable.getDefaultCell().setBorderColor(new BaseColor(0, 0, 0));//边框颜色

        inttable.getDefaultCell().setUseAscender(true);//设置可以居中
        inttable.getDefaultCell().setMinimumHeight(20);////单元格内文字不确定个，不能设置成固定行高
//		inttable.setHeaderRows(1);//第一行做表头 会导致每一页开头都有表头
        inttable.setSplitLate(false);
        inttable.setSplitRows(true);
        //表头
        for(int i=0; i<5;i++){
            inttable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);//设置水平居中
            inttable.getDefaultCell().setVerticalAlignment(Element.ALIGN_CENTER);//设置垂直居中
            inttable.addCell(new Paragraph((String) colName.get(i),getTableFont()));
        }

        //表格每行
        for(int i = 0; i < items.size(); i++){
            for(int j = 0; j<((List)items.get(i)).size(); j++){
                if(j == 0){
                    inttable.getDefaultCell().setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
                }else{
                    inttable.getDefaultCell().setBorder(Rectangle.RIGHT | Rectangle.BOTTOM);
                }
                if(j < colName.size()){
                    inttable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);//设置水平居中
                    inttable.getDefaultCell().setVerticalAlignment(Element.ALIGN_CENTER);//设置垂直居中
                    inttable.addCell(new Paragraph(((List)items.get(i)).get(j).toString(),getTableFont()));
                }
            }
        }
        return inttable;
    }

    private static Font getTableFont() throws DocumentException, IOException {
        BaseFont baseFont = BaseFont.createFont("C:/Windows/Fonts/simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        Font font = new Font(baseFont,10f,Font.NORMAL,new BaseColor(0, 0, 0));
        font.setFamily("仿宋_GB2312");
        return font;
    }
}
